package com.hmkj.web.controller.pay;

import com.hmkj.common.entity.ResultEntity;
import com.hmkj.common.utils.StringUtils;
import com.hmkj.core.constant.ConfigNID;
import com.hmkj.core.exception.BussinessException;
import com.hmkj.core.model.amount.AccountModel;
import com.hmkj.core.po.amount.Recharge;
import com.hmkj.core.po.user.User;
import com.hmkj.core.po.user.UserFreeze;
import com.hmkj.core.service.amount.AccountService;
import com.hmkj.core.service.amount.RechargeService;
import com.hmkj.core.service.operate.AgentService;
import com.hmkj.core.service.user.UserFreezeService;
import com.hmkj.core.service.user.UserService;
import com.hmkj.web.controller.GenericController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 支付宝转账
 */
@Api(description = "支付宝转账相关")
@Slf4j
@RestController
@RequestMapping(GenericController.MEMBER_URL + "/pay/alipay")
public class AliPayController extends GenericController<Integer, Recharge> {
    @Autowired
    UserService userService;
    @Autowired
    RechargeService rechargeService;
    @Autowired
    AccountService accountService;
    @Autowired
    AgentService agentService;
    @Autowired
    UserFreezeService userFreezeService;


    @ApiOperation(value = "支付宝账号信息", notes = "支付宝账号信息")
    @ResponseBody
    @RequestMapping(value = "aliPayAccount", method = RequestMethod.POST)
    public ResultEntity aliPayAccount() {
        User user = getLoginUser();
        if (!StringUtils.isNotBlank(user.getRealName()) || !StringUtils.isNotBlank(user.getCode())) {
            throw new BussinessException("未实名认证");
        }
        if (!StringUtils.isNotBlank(user.getAlipayAccount())) {
            throw new BussinessException("未绑定支付宝账号");
        }
        AccountModel model = accountService.getAccountModel(user.getId());
        Map<String, Object> map = new HashMap<>();
        String realName = user.getRealName().substring(0, user.getRealName().length() - 1) + "*";
        String alipayAccount = user.getAlipayAccount().charAt(0) + "****" + user.getAlipayAccount().charAt(user.getAlipayAccount().length() - 1);
        map.put("realName", realName);
        map.put("alipayAccount", alipayAccount);
        map.put("available", model.getAvailable());
        map.put("rechargeMin", redisService.get(ConfigNID.RECHARGE_MIN));
        map.put("rechargeMax", redisService.get(ConfigNID.RECHARGE_MAX));
        return ok(map);
    }


    @ApiOperation(value = "支付宝充值", notes = "支付宝充值")
    @ResponseBody
    @RequestMapping(value = "alipayRecharge", method = RequestMethod.POST)
    public ResultEntity alipayRecharge(@ApiParam("充值金额") @RequestParam final BigDecimal amount) {
        User user = userService.selectByPrimaryKey(getLoginUser().getId());
        UserFreeze userFreeze = userFreezeService.selectOne(new UserFreeze(f -> {
            f.setDelFlag(UserFreeze.DELFLAG.NORMAL.code);
            f.setUserId(user.getId());
        }));
        if (null == userFreeze || UserFreeze.STATUS.T1.code != userFreeze.getFreezeRecharge()) {
            throw new BussinessException("您的账户充值功能已被冻结，请联系客服处理");
        }
        rechargeService.doAlipayRecharge(user, amount);
        // 需要后台审核
        return ok(m -> {
            m.put("alipayUrl", redisService.get(ConfigNID.ALIPAY_URL));
        });

    }

    @ApiOperation(value = "转账信息", notes = "转账信息")
    @ResponseBody
    @RequestMapping(value = "rechargeInfo", method = RequestMethod.POST)
    public ResultEntity aliPayRecharge() {
        String alipayAccount = redisService.get(ConfigNID.ALIPAY_ACCOUNT);
        String alipayName = redisService.get(ConfigNID.ALIPAY_NAME);
        Map<String, Object> map = new HashMap<>();
        map.put("alipayName", alipayName);
        map.put("alipayAccount", alipayAccount);
        return ok(map);
    }

}
